package com.cskaoyan.mall.controller.mall;

import com.cskaoyan.mall.model.bo.AdminLoginBO;
import com.cskaoyan.mall.model.bo.SettleAccountsBO;
import com.cskaoyan.mall.model.sql.Result;
import com.cskaoyan.mall.model.sql.User;
import com.cskaoyan.mall.model.vo.AdminLoginVO;
import com.cskaoyan.mall.model.vo.OrderByStateVO;
import com.cskaoyan.mall.model.vo.UserSignUpVO;
import com.cskaoyan.mall.service.OrderService;
import com.cskaoyan.mall.service.UserService;
import com.cskaoyan.mall.service.implement.OrderServiceImpl;
import com.cskaoyan.mall.service.implement.UserServiceImpl;
import com.cskaoyan.mall.utils.HttpUtils;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/api/mall/order/*")
public class OrderServlet extends HttpServlet {

    private UserService userService = new UserServiceImpl();

    private OrderService orderService = new OrderServiceImpl();

    Gson gson = new Gson();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String action = requestURI.replace("/api/mall/order/", "");
        if ("getOrderByState".equals(action)){
            getOrderByState(req,resp);
        }else if ("pay".equals(action)){
            pay(req,resp);
        }else if ("confirmReceive".equals(action)){
            confirmReceive(req,resp);
        }
    }

    /**
     * 确认收货
     * @param req
     * @param resp
     */
    private void confirmReceive(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        orderService.confirmReceive(id);
        resp.getWriter().println(gson.toJson(Result.ok()));
    }

    /**
     * 确认付款 id=4
     * @param req
     * @param resp
     */
    private void pay(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        orderService.pay(id);
        resp.getWriter().println(gson.toJson(Result.ok()));
    }

    /**
     * 获取订单信息  根据state 和 token 获取
     * @param req
     * @param resp
     */
    private void getOrderByState(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String state = req.getParameter("state");
        String token = req.getParameter("token");
        List<OrderByStateVO> orderByStateVOList = userService.getOrderByState(state,token);
        resp.getWriter().println(gson.toJson(Result.ok(orderByStateVOList)));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String action = requestURI.replace("/api/mall/order/", "");
        if("settleAccounts".equals(action)){
            settleAccounts(req,resp);
        }else if ("sendComment".equals(action)){
            sendComment(req,resp);
        }
    }

    /**
     * 评价{"token":"admin","orderId":1963,"goodsId":511,"goodsDetailId":1312,"content":"二胡","score":100}
     * @param req
     * @param resp
     */
    private void sendComment(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //get orderId 并且修改---> hasComment = 1
        orderService.sendComment();
        resp.getWriter().println(gson.toJson(Result.ok()));
    }

    /**
     * 确认下单
     * @param req
     * @param resp
     */
    private void settleAccounts(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = HttpUtils.getRequestBody(req);
        SettleAccountsBO accountsBO = gson.fromJson(requestBody, SettleAccountsBO.class);
        orderService.settleAccounts(accountsBO);
        resp.getWriter().println(gson.toJson(Result.ok()));
    }

}
